# Define packages
packages = c("dplyr","tidyr",                                  # data manipulation
             "rgdal","maptools",                               # shapefiles
             "leaflet","leaflet.extras","leaflet.minicharts",  # maps
             "RColorBrewer",                                   # colors palettes
             "plyr",                                           # easy merge
             "stringr")                                        # strings manipulation
                                                     
# Attach packages
invisible(suppressMessages(lapply(packages, library, character.only = T)))

# Define working directory
csv_cd = "C:/Users/lgoye/OneDrive/documents/GitHub/hw02_lgoyenec/data_csv"
shp_cd = "C:/Users/lgoye/OneDrive/documents/GitHub/hw02_lgoyenec/data_shp"

# Import data
crimes = read.csv(paste0(csv_cd, "/crimes_2018.csv"))
police = read.csv(paste0(csv_cd, "/police_stations.csv"))
  
# Import shapefiles
sh_bound = readOGR(dsn = shp_cd, 
                   layer = "police_beats", 
                   verbose = F)
sh_bikes = readOGR(dsn = shp_cd, 
                   layer = "bike_routes", 
                   verbose = F)
# All variables in lower case
names(crimes) = tolower(names(crimes))
names(police) = tolower(names(police))

# Choosing variables of interest
# Crime: 
  # Select longitud, latitud, type of crime and other geo categories
crimes = 
  crimes %>% 
  select(date, 
         primary.type, 
         location.description, 
         arrest, domestic, 
         beat, district, ward, community.area, 
         x.coordinate, y.coordinate, latitude, longitude) 

# Police:
  # Select longitud, latitud, and other geo categories
police = 
  police %>%
  select(`ï..district`, district.name, 
         wards,
         x.coordinate, y.coordinate, latitude, longitude) %>%
  dplyr::rename(ward = wards) %>%
  
  # Changes on district variable for polylines map
  dplyr::rename(district = `ï..district`) %>%
  mutate(district = as.numeric(ifelse(district == "Headquarters", 0, district)))

# Construct final data based on interest of analysis:: 
  # Select Primary Type of interest and create categorical variable:
      # Homicide
      # Human Trafficking 
      # Kidnapping
      # Narcotics

data = 
  crimes %>%
  filter(
    primary.type %in% 
      c("HOMICIDE", 
      "HUMAN TRAFFICKING", 
      "KIDNAPPING", 
      "PROSTITUTION")
  ) %>%
  mutate(
    crime_type =
      ifelse(primary.type == "HOMICIDE", 1, 
      ifelse(primary.type == "HUMAN TRAFFICKING", 2,
      ifelse(primary.type == "KIDNAPPING", 3, 4)))
  )

Maps

Map 1:

Distribution of total crimes in 2018 across police beats

Map 2

Distribution of homicides, human traffickig, Kidnapping and Prostitution in 2018 across Chicago

Map 3

Distance between police stations and assault crimes

# Select assaults that ended up in an arrest
data_kidna =
  crimes %>%
  filter(primary.type == "ASSAULT", arrest == "true") %>%
  select(latitude, longitude) 

leaflet() %>%
  
  # Add base maps
  addProviderTiles(provider = "Esri.WorldGrayCanvas", group = "World Gray") %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles("CartoDB.DarkMatterNoLabels"     , group = "World Dark") %>% 
  addProviderTiles(provider = "Esri.WorldStreetMap" , group = "World StreetMap") %>%
  addProviderTiles(provider = "Esri.WorldImagery"   , group = "World Imagery") %>%
  
  # Add polygons of police beats
  addPolygons(data = sh_bound, 
              color = "white", weight = 1, 
              fillColor = "#202030", fillOpacity = 0.6) %>%
  
  # Bike routes across Chicago
  addPolylines(data = sh_bikes, 
               color = "#F5BB00",
               weight = 1.5,
               group = "Bike Routes") %>%
  
  # Crimes Markers: points where assault crimes happened
  addCircleMarkers(data = data_kidna,
                   lng =~ longitude, lat =~ latitude,
                   color = "#BA1200", fillOpacity = 1,
                   radius = 0.4, stroke = F) %>%
  
  # Police Markers: points with radius 
  addCircleMarkers(data = police,
                   lng =~ longitude, lat =~ latitude,
                   color = "#2A4494", fillOpacity = 0.6,
                   radius = 15, stroke = T, weight = 1.5,
                   group = "Police Radius") %>%
  
  # Add Layer Control for all the basemaps and 2 layers
  # The layers corresponds to:
    # Police stations with their specific radius
    # Bike roads across the city
  addLayersControl(
    baseGroups = c("World Gray","OSM (default)","World Dark","World StreetMap","World Imagery"),
    overlayGroups = c("Police Radius", "Bike Routes"),
    options = layersControlOptions(collapsed = F),
    position = "bottomright") %>%
  
  # Hide the layer: Bike Routes
    # In this way we avoid having to much information in a single map
  hideGroup("Bike Routes")